import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/routers/app_routes.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class StrUtils {

  // 获取对应性别图片
  static getSexImage(int? sex) {
    ///0=男,1=女,2=未知
    if (sex == 1) {
      return 'assets/images/img_list_wm.png';
    } else {
      return 'assets/images/img_list_man.png';
    }
  }

  // 获取对应等级图片
  static getLevelImage(int? level) {
    String levelImage;
    switch (level) {
      case 1:
        levelImage = 'assets/images/img_dj1.png';
        break;
      case 2:
        levelImage = 'assets/images/img_dj2.png';
        break;
      case 3:
        levelImage = 'assets/images/img_dj3.png';
        break;
      case 4:
        levelImage = 'assets/images/img_dj4.png';
        break;
      case 5:
        levelImage = 'assets/images/img_dj5.png';
        break;
      case 6:
        levelImage = 'assets/images/img_dj6.png';
        break;
      case 7:
        levelImage = 'assets/images/img_dj7.png';
        break;
      case 8:
        levelImage = 'assets/images/img_dj8.png';
        break;
      case 9:
        levelImage = 'assets/images/img_dj9.png';
        break;
      default:
        levelImage = 'assets/images/img_dj0.png';
        break;
    }
    return levelImage;
  }


  static Color getBackgroundColor(String? rules) {
    var color = const Color(0x0f499265);
    if (rules == 'SEVENS') {
      color = const Color(0x0f002CFF);
    } else if (rules == 'EIGHT_PERSON') {
      color = const Color(0x0f00BBFF);
    } else if (rules == 'ELEVEN_PERSON') {
      color = const Color(0x0fFF9100);
    }
    return color;
  }

  static Color getTextColor(String? rules) {
    var color = const Color(0xff499265);
    if (rules == 'SEVENS') {
      color = const Color(0xff002CFF);
    } else if (rules == 'EIGHT_PERSON') {
      color = const Color(0xff00BBFF);
    } else if (rules == 'ELEVEN_PERSON') {
      color = const Color(0xffFF9100);
    }
    return color;
  }


  static Color getColorByCode(String? code) {
    Color color = const Color(0xff499360);
    if (code == 'GUARD') {
      color = const Color(0xff00BBFF);
    } else if (code == 'FREE_MAN') {
      color = const Color(0xff499360);
    } else if (code == 'GOALKEEPER') {
      color = const Color(0xffFF9100);
    } else if (code == 'MIDFIELD') {
      color = const Color(0xff002CFF);
    }
    return color;
  }


  // 比较版本号
  static bool isUpdateVersion(String newVersion, String old) {
    if (newVersion == '' || newVersion.isEmpty || old == '' || old.isEmpty) {
      return false;
    }


    // logger.d('====================${newVersion}========${old}');
    int newVersionInt, oldVersion;
    var newList = newVersion.split('.');
    var oldList = old.split('.');
    if (newList.isEmpty || oldList.isEmpty) {
      return false;
    }

    if (newVersion == old) {
      return false;
    }
    
    for (int i = 0; i < newList.length; i++) {
      newVersionInt = int.parse(newList[i]);
      oldVersion = int.parse(oldList[i]);
      if (newVersionInt > oldVersion) {
        return true;
      } else if (newVersionInt < oldVersion) {
        return false;
      }
    }
    return false;
  }



  static roleTextColor(String clubRoleCode) {
    if (clubRoleCode == "CHAIRMAN") {
      return const Color(0xffFF9100);
    } else if (clubRoleCode == "ACCOUNTING") {
      return const Color(0xff00BBFF);
    } else if (clubRoleCode == "ADMINISTRATORS") {
      return const Color(0xff002CFF);
    } else {
      return const Color(0xff499360);
    }
  }

  /// 格式化时间
  static String formatYNDHM(DateTime time) {
    return DateFormat('yyyy-MM-dd hh:mm').format(time);
  }

  // 跳转方式
  static jumpByUrl(String? url, String? type) {
    if (type == null || url == null || url.isEmpty) {
      return;
    }
    switch (type) {
      case CustomConstant.BANNER_TYPE_APP:
        {
          Uri uri = Uri.parse(url);
          String? targetPage = uri.queryParameters['targetPage'];
          if (targetPage != null && targetPage.isNotEmpty) {
            if (targetPage == 'LocalCourt') {
              Get.toNamed(AppRoutes.localCourse);
            } else if (targetPage == 'ClubRanking') {
              Get.toNamed(AppRoutes.clubRanking);
            } else if (targetPage == 'HallOfFame') {
              Get.toNamed(AppRoutes.hallOfFame);
            }
          }
          break;
        }
      case CustomConstant.BANNER_TYPE_H5:
        {
          // Get.to(() => WebViewWidget(url: url));
          break;
        }
      case CustomConstant.BANNER_TYPE_LINK:
        {
          // launchWithUrl(url);
          break;
        }
    }
  }

}